package com.egg.ylt.adapter.record;

import android.text.SpannableStringBuilder;
import android.view.View;

import com.egg.ylt.R;
import com.egg.ylt.Utils.StringSpanUtils;
import com.egg.ylt.pojo.BabyDynamicMsgEntity;
import com.egg.ylt.presenter.impl.RecordPresenter;
import com.yonyou.framework.library.adapter.rv.ItemViewDelegate;
import com.yonyou.framework.library.adapter.rv.ViewHolder;

/**
 * Created By Dlx
 * on 2019/1/19
 * view type = "4"
 */
public class RecordAutoUploadCalorieViewType implements ItemViewDelegate<BabyDynamicMsgEntity.ListBean> {

    private RecordPresenter mPresenter;

    public RecordAutoUploadCalorieViewType(RecordPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_record_cal;
    }

    @Override
    public boolean isForViewType(BabyDynamicMsgEntity.ListBean item, int position) {
        if ("4".equals(item.getRecordType())) {
            return true;
        }
        return false;
    }

    @Override
    public void convert(ViewHolder holder, final BabyDynamicMsgEntity.ListBean babyDynamicMsgEntity, final int position) {
        holder.setOnClickListener(R.id.item_record_autocal_deleteiv, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.deleteBabyDynamicMsg(babyDynamicMsgEntity.getId(), 0, position);
            }
        });
        holder.setOnClickListener(R.id.item_record_autocal_sharediv, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.shareDynamic(babyDynamicMsgEntity.getId(), 0, position);
            }
        });
        holder.setText(R.id.record_date_textview, babyDynamicMsgEntity.getDate());
        holder.setText(R.id.record_date_days_textview, babyDynamicMsgEntity.getWeek());
        holder.setSpanText(R.id.item_record_autocal_swimtimetv, formatMMss(babyDynamicMsgEntity.getTimeLength()));
        holder.setText(R.id.item_record_autocal_costtv, babyDynamicMsgEntity.getCalories());
        holder.setText(R.id.item_record_autocal_datetv, babyDynamicMsgEntity.getTime());
        holder.setText(R.id.item_record_autocal_shopnametv, babyDynamicMsgEntity.getShopName());
    }

    private SpannableStringBuilder formatMMss(String timeStr) {
        if ("".equals(timeStr) || timeStr == null)
            return StringSpanUtils.init().addForegroundColorHighLight(R.color.red, "数据错误").build();
        int temp = Integer.valueOf(timeStr);
        int minute = temp / 60;
        int second = temp % 60;
        return StringSpanUtils.init().addStr("累计时长")
                .addForegroundColorHighLight(mPresenter.getContext().getResources().getColor(R.color.ylt_style_orange), String.valueOf(minute))
                .addStr("分钟")
                .addForegroundColorHighLight(mPresenter.getContext().getResources().getColor(R.color.ylt_style_orange), String.valueOf(second))
                .addStr("秒").build();
    }
}
